package com.project.utils.pagination;

import com.project.myBeans.EntitySupport;
import net.sf.ezmorph.object.DateMorpher;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
import net.sf.json.util.JSONUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by g on 2015/5/31.
 */
public class CopyDto {
    /**
     * 把前台所有出入的值拷贝到传入的entity对象中
     * @param req
     * @param obj   entity对象
     */
    public static void copyDto(HttpServletRequest req , Object obj){
        Map<?,?> map = req.getParameterMap();//得到前台所有的值，类型是map
        Map<String, Object> tempMap = new HashMap<String, Object>();//记录单一属性值
        Iterator<?> ite = map.entrySet().iterator();
        while(ite.hasNext()){
            Map.Entry<?,?> pairs = (Map.Entry<?,?>)ite.next();
            String key = (String)pairs.getKey();
            Object o= pairs.getValue();//是一个数组对象
            try {
                setProperty(obj, key, o,tempMap);
            } catch (Exception e) {

            }
        }
        if(!tempMap.isEmpty()){
            Iterator<?> it = tempMap.entrySet().iterator();
            while(it.hasNext()){
                Map.Entry<String,?> pairs = (Map.Entry<String,?>)it.next();
                if(pairs.getValue()!=null){
                    ClassHelper.setProperty(obj, pairs.getKey(), pairs.getValue());
                }
            }
        }
    }

    private static void setProperty(Object obj,String key ,Object o,Map<String, Object> tempMap) throws Exception{
        String[] strs = (String[])o;
        for (int i = 0; i < strs.length; i++) {
            try {
                String[] dateFormats = new String[] {"yyyy-MM-dd HH:mm:ss","yyyy-MM-dd"};
                JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpher(dateFormats));
                Object json = (JSONObject) JSONSerializer.toJSON(strs[i]);
                if(json instanceof JSONObject){//如果是jsonObject对象
                    ClassHelper.copyProperties(JSONObject.toBean((JSONObject)json, obj.getClass()), obj);
                }else if(json instanceof JSONArray){//得到的是一个json数组
                    JSONArray jsons = (JSONArray)json;
                    for(int j = 0 ; j < jsons.size() ; j++){
                        JSONObject json2 = jsons.getJSONObject(i);
                        ClassHelper.copyProperties(JSONObject.toBean( json2, obj.getClass()), obj);
                    }
                }else{
                    tempMap.put(key, strs[i]);//把页面key属性的值赋到entity对象中
                }
            } catch (Exception e) {
                try {
                    if(strs[i]!=null && !"".equals(strs[i]) && !"null".equals(strs[i])){
                        tempMap.put(key, strs[i]);//把页面key属性的值赋到entity对象中
                    }
                } catch (Exception e2) {
                    e2.printStackTrace();
                }
            }
        }
    }

    /**
     * 获取数据返回记录数
     *
     * @param entity
     * @return
     */
    public static int getTotal(EntitySupport entity) {
        return entity.getTotal();
    }
}
